#pragma once

#include<iostream>
#include<vector>
#include<unordered_map>
#include<fstream>
#include<cassert>


#include"../comm/log.hpp"
#include"../comm/util.hpp"

namespace ns_model
{
    using namespace std;
    using namespace ns_log;
    using namespace ns_util;

    struct Question
    {
        std::string number;//题目编号
        std::string title; //题目标题
        std::string star;  //题目难度
        int cpu_limit;     //题目的时间要求(s)
        int mem_limit;     //题目的空间要求(KB)
        std::string desc;  //题目的描述
        std::string header;//题目预设给用户在线编辑器的代码
        std::string tail;  //题目的测试用例，需要和header拼接，形成完整代码
    };

    std::string questions_list = "./questions/questions.list";
    std::string questions_path = "./questions/";

    class Model
    {
    private:
        unordered_map<string,Question> questions;
    public:
        Model()
        {
            assert(LoadQuestionList(questions_list));
        }
        ~Model()
        {}

        bool LoadQuestionList(const string& question_list)
        {
            //加载配置文件 /questions/questions.list + 题目编号文件
            //就是填充questions
            fstream in(question_list);
            if(!in.is_open())
            {
                LOG(FATAL) << "加载题库失败，请检查是否存在题库文件" << "\n";
                return false;
            }
            //按行读取
            string line;
            while(getline(in,line))
            {
                vector<string> tokens;
                StringUtil::SplitString(line,&tokens," ");

                if(tokens.size() != 5)
                {
                    LOG(WARNING) << "加载部分题目失败，请检查文件格式" << "\n";
                    continue;
                }

                //填充字段
                //1 回文数 简单 1 30000
                Question q;
                q.number = tokens[0];
                q.title = tokens[1];
                q.star = tokens[2];
                q.cpu_limit = stoi(tokens[3]);
                q.mem_limit = stoi(tokens[4]);

                //填充题目具体描述，header和tail文件内容
                string path = questions_path;
                path += q.number;
                path += "/";
                
                //路径为/questions/1/
                FileUtil::ReadFile(path + "desc.txt", &(q.desc), true);
                FileUtil::ReadFile(path + "head.cpp", &(q.header), true);
                FileUtil::ReadFile(path + "tail.cpp", &(q.tail), true);

                questions.insert({q.number,q});
            }

            LOG(INFO) << "加载题库...成功" << "\n";
            in.close();

            return true;
        }

        bool GetAllQuestions(vector<Question> *out)
        {
            //获取所有文件
            if(questions.size() == 0)
            {
                LOG(ERROR) << "用户获取题库失败" << "\n";
                return false;
            }
            for(const auto& q: questions)
            {
                out->push_back(q.second);
            }

            return true;
        }

        bool GetOneQuestion(const std::string& number, Question* q)
        {
            //获取单个文件
            auto iter = questions.find(number);
            if(iter == questions.end())
            {
                LOG(ERROR) << "用户获取题目失败，题号是： " << number << "\n";
                return false;
            }
            *q = iter->second;
            return true;
        }

    };

}